﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using FollowUpNetwork.Models;
using PagedList;

namespace FollowUpNetwork.Controllers
{
    [Authorize(Roles = "Vip")]
    public class CameraLeaderboardsController : Controller
    {
        private FollowUpNetworkContext db = new FollowUpNetworkContext();

        // GET: CameraLeaderboards
        public ActionResult Index(string sortBy, string currentSort, int? page)
        {
            int pageIndex = 1;
            int pageSize = 20;
            pageIndex = page.HasValue ? Convert.ToInt32(page) : 1;
            ViewBag.CurrentSort = sortBy;
            sortBy = string.IsNullOrEmpty(sortBy) ? "Name" : sortBy;
            IPagedList<CameraLeaderboard> lstStudent = null;
            switch (sortBy)
            {
                case "CameraRanking":
                    //如果sortBy==currentSort，就按照对应字段降序排列，并分页。否则升序。
                    if (sortBy.Equals(currentSort))
                    {
                        lstStudent = db.Set<CameraLeaderboard>().
                         OrderByDescending(s => s.CameraRanking).
                         ToPagedList(pageIndex, pageSize);
                    }
                    else
                    {
                        lstStudent = db.Set<CameraLeaderboard>().
                         OrderBy(s => s.CameraRanking).
                         ToPagedList(pageIndex, pageSize);
                    }
                    break;
                case "CameraPrice":
                    //如果sortBy==currentSort，就按照对应字段降序排列，并分页。否则升序。
                    if (sortBy.Equals(currentSort))
                    {
                        lstStudent = db.Set<CameraLeaderboard>().
                         OrderByDescending(s => s.CameraPrice).
                         ToPagedList(pageIndex, pageSize);
                    }
                    else
                    {
                        lstStudent = db.Set<CameraLeaderboard>().
                         OrderBy(s => s.CameraPrice).
                         ToPagedList(pageIndex, pageSize);
                    }
                    break;

                default:
                    //如果sortBy==currentSort，就按照对应字段降序排列，并分页。否则升序。
                    if (sortBy.Equals(currentSort))
                    {
                        lstStudent = db.Set<CameraLeaderboard>().
                         OrderByDescending(s => s.CameraRanking).
                         ToPagedList(pageIndex, pageSize);
                    }
                    else
                    {
                        lstStudent = db.Set<CameraLeaderboard>().
                         OrderBy(s => s.CameraRanking).
                         ToPagedList(pageIndex, pageSize);
                    }

                    break;
            }
            return View(lstStudent);
        }

        // GET: CameraLeaderboards/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            CameraLeaderboard cameraLeaderboard = db.CameraLeaderboards.Find(id);
            if (cameraLeaderboard == null)
            {
                return HttpNotFound();
            }
            return View(cameraLeaderboard);
        }

        // GET: CameraLeaderboards/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: CameraLeaderboards/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "CameraLeaderboardId,CameraRanking,CameraType,CameraPrice")] CameraLeaderboard cameraLeaderboard)
        {
            if (ModelState.IsValid)
            {
                db.CameraLeaderboards.Add(cameraLeaderboard);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(cameraLeaderboard);
        }

        // GET: CameraLeaderboards/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            CameraLeaderboard cameraLeaderboard = db.CameraLeaderboards.Find(id);
            if (cameraLeaderboard == null)
            {
                return HttpNotFound();
            }
            return View(cameraLeaderboard);
        }

        // POST: CameraLeaderboards/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "CameraLeaderboardId,CameraRanking,CameraType,CameraPrice")] CameraLeaderboard cameraLeaderboard)
        {
            if (ModelState.IsValid)
            {
                db.Entry(cameraLeaderboard).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(cameraLeaderboard);
        }

        // GET: CameraLeaderboards/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            CameraLeaderboard cameraLeaderboard = db.CameraLeaderboards.Find(id);
            if (cameraLeaderboard == null)
            {
                return HttpNotFound();
            }
            return View(cameraLeaderboard);
        }

        // POST: CameraLeaderboards/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            CameraLeaderboard cameraLeaderboard = db.CameraLeaderboards.Find(id);
            db.CameraLeaderboards.Remove(cameraLeaderboard);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
